package com.clstu.qqserver.service;

import java.util.HashMap;
import java.util.Set;

/**
 * 该类用于管理所以与用户连接的服务器线程
 */
public class ServerConnectClientThreadManager {

    /** key:userId -> value:该用户的连接线程 */
    private static final HashMap<String,ServerConnectClientThread> userConnectionThreadMap = new HashMap<>();

    public static HashMap<String, ServerConnectClientThread> getUserConnectionThreadMap() {
        return userConnectionThreadMap;
    }

    public static void addServerConnectClientThread(String userID, ServerConnectClientThread s){
        userConnectionThreadMap.put(userID,s);
    }

    public static ServerConnectClientThread getServerConnectClientThread(String userID){
        return userConnectionThreadMap.get(userID);
    }

    public static void removeClientConnectServerThread(String userID){
        userConnectionThreadMap.remove(userID);
    }

    //获取在线用户列表
    public static String getOline(){
        Set<String> userIds = userConnectionThreadMap.keySet();//获取所有的用户名
        StringBuilder listStr = new StringBuilder();
        userIds.forEach(userID->listStr.append(userID).append(" "));
        return listStr.toString();
    }
}
